package com.lemon.smartsafety.utils.app;

import android.app.Activity;
import android.os.Handler;
import android.widget.ImageView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.lemon.smartsafety.BuildConfig;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;

/**
 * Created by Cooliean on 16/1/20.
 * KProgressHUD 工具
 */
public class HUDUtil {
    private LoggerHelper loggerHelper = new LoggerHelper(HUDUtil.class);

    private Activity      mContext;
    private KProgressHUD hud;

    //private LayoutInflater mInflater;

    private static int DEFAULT_DURATION = Constants.DEFAULT_HUD_DURATION;

    public HUDUtil() {
    }

//    private HUDUtil(Context context) {
//        this.mContext = context;
//        //this.mInflater = LayoutInflater.from(context);
//        hud = KProgressHUD.create(context).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
//    }

    private HUDUtil(Activity context) {
        this.mContext = context;
        //this.mInflater = LayoutInflater.from(context);
        hud = KProgressHUD.create(context).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @param context
     * @return
     */
    public  HUDUtil init(Activity context) {
        return new HUDUtil(context);
    }



    /**
     * 重置并显示
     *
     * @return
     */
    public synchronized HUDUtil showHUD() {
        try {
            if (mContext != null && hud != null && !mContext.isFinishing() && hud.isShowing()) {
                hud.dismiss();
            }
            this.setDefault();

            hud.show();
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                e.printStackTrace();
            }
        } finally {
            LogUtils.LOGE("hud.show()");
            return this;
        }
    }

    /**
     * 重置并显示
     *
     * @param message 显示的消息内容
     * @return
     */
    public synchronized HUDUtil showHUD(String message) {
        if (mContext != null && hud != null && !mContext.isFinishing() && hud.isShowing()) {
            hud.dismiss();
        }
        this.setDefault();
        this.setLabel(message);
        hud.show();
        return this;
    }

    public synchronized HUDUtil showErrorTip(int res) {
        return showErrorTip(this.mContext.getResources().getString(res), DEFAULT_DURATION, null);
    }

    public synchronized HUDUtil showErrorTip(String message) {
        return showErrorTip(message, DEFAULT_DURATION, null);
    }

    public synchronized HUDUtil showErrorTip(String message, CallBack callBack) {
        return showErrorTip(message, DEFAULT_DURATION, callBack);
    }

    public synchronized HUDUtil showErrorTip(String message, int duration, CallBack callBack) {
        showHUD();
        return showTip(R.mipmap.ic_error_circle, message, duration, callBack);
    }


    public synchronized HUDUtil showInfoTip(String message) {
        return showInfoTip(message, DEFAULT_DURATION, null);
    }

    public synchronized HUDUtil showInfoTip(String message, CallBack callBack) {
        return showInfoTip(message, DEFAULT_DURATION, callBack);
    }

    public synchronized HUDUtil showInfoTip(String message, int duration, CallBack callBack) {
        showHUD();
        return showTip(R.mipmap.ic_info, message, duration, callBack);
    }


    public synchronized HUDUtil showDoneTip(int res) {
        return showDoneTip(this.mContext.getString(res), DEFAULT_DURATION, null);
    }

    /**
     * 显示完成提示，DEFAULT_DURATION后会自动关闭
     *
     * @param message
     * @return
     */
    public synchronized HUDUtil showDoneTip(String message) {
        return showDoneTip(message, DEFAULT_DURATION, null);
    }

    /**
     * 显示开发中
     *
     * @return
     */
    public synchronized HUDUtil showDeveloper() {
        showHUD();
        showTip(R.mipmap.ic_smile, "功能开发中...", DEFAULT_DURATION, null);
        return this;
    }


    public synchronized HUDUtil showDoneTip(String message, CallBack callBack) {
        return showDoneTip(message, DEFAULT_DURATION, callBack);
    }

    public synchronized HUDUtil showDoneTip(String message, int duration, CallBack callBack) {
        showHUD();
        return showTip(R.mipmap.ic_check_circle, message, duration, callBack);
    }

    public synchronized HUDUtil showTip(int res, String message, int duration, CallBack callBack) {
        ImageView imageView = new ImageView(this.mContext);
        imageView.setImageResource(res);
        hud.setCustomView(imageView).setLabel(message);
        if (duration > 0) {
            scheduleDismiss(duration, callBack);
        }
        show();
        return this;
    }


    public synchronized HUDUtil hiddenDelay() {
        hiddenDelay(DEFAULT_DURATION, null);
        return this;
    }

    public synchronized HUDUtil hiddenDelay(CallBack callBack) {
        hiddenDelay(DEFAULT_DURATION, callBack);
        return this;
    }


    public synchronized HUDUtil hiddenDelay(int mill, CallBack callBack) {
        if (hud != null && hud.isShowing()) {
            scheduleDismiss(mill, callBack);
        }
        return this;
    }

    public synchronized HUDUtil hidden() {
        if (mContext != null && hud != null && !mContext.isFinishing() && hud.isShowing()) {
            hud.dismiss();
        }
        return this;
    }

    public synchronized HUDUtil show() {
        if (hud.isShowing()) {
            return this;
        }
        hud.show();
        return this;
    }

    /**
     * 多少<strong>duration</strong> 毫秒后消失
     *
     * @param duration
     * @param callBack
     */
    private synchronized void scheduleDismiss(int duration, final CallBack callBack) {
        try {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mContext != null && hud != null && !mContext.isFinishing()) {
                        hud.dismiss();
                    }
                    if (callBack != null) {
                        callBack.onCall();
                    }
                }
            }, duration);
        } catch (Exception e) {

        }

    }


    /**
     * 是否使用主题颜色
     *
     * @param bool
     * @return
     */
    public HUDUtil isPrimary(boolean bool) {
        if (bool) {
            hud.setWindowColor(this.mContext.getResources().getColor(R.color.colorPrimary));
        }
        return this;
    }


    /**
     * 设置动画速度
     *
     * @param scale
     * @return
     */
    public HUDUtil setAnimationSpeed(int scale) {
        hud.setAnimationSpeed(scale);
        return this;
    }

    /**
     * 模态的程度
     *
     * @param dimAmount 0 不模态，1最大
     * @return
     */
    public HUDUtil setDimAmount(float dimAmount) {
        if (dimAmount >= 0 && dimAmount <= 1) {
            hud.setDimAmount(dimAmount);
        }
        return this;
    }

    /**
     * 设置模态
     *
     * @param bool
     * @return
     */
    public HUDUtil isModel(boolean bool) {
        if (bool) {
            hud.setDimAmount(0.5f);
        }
        return this;
    }

    /**
     * 设置message
     *
     * @param label
     * @return
     */
    public synchronized HUDUtil setLabel(String label) {
        hud.setLabel(label);
        return this;
    }


    /**
     * 回复默认的HUD样式
     *
     * @return
     */
    public synchronized HUDUtil setDefault() {
        if (this.mContext != null && hud != null && !this.mContext.isFinishing()) {
            hud.dismiss();
        }
        hud = KProgressHUD.create(mContext).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
        if (Constants.IS_HUD_USER_PRIMARY) {
            this.isPrimary(true);
        } else {
            hud.setWindowColor(this.mContext.getResources().getColor(com.kaopiz.kprogresshud.R.color.kprogresshud_default_color));
        }
        return this;
    }

    //界面移出屏幕时接口回调
    public interface CallBack {
        void onCall();
    }
}
